<template>
  <el-dialog title="分配角色" :visible="dialogRole" @close="closeFn">
    <!-- 复选框组件 -->
    <el-checkbox-group v-model="RolecheckList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.title }}
      </el-checkbox>
    </el-checkbox-group>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button
        type="primary"
        @click="comfirmFn(userId)"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { roleList } from '@/api/rolemanage'
import { employeeDatail, editEmployee } from '@/api/employees'
export default {
  props: {
    dialogRole: {
      type: Boolean,
      default: false
    },
    userId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      RolecheckList: [],
      roleList: [] // 角色列表
    }
  },
  async created() {
    // 获取角色列表
    const res = await roleList()
    this.roleList = res
    // console.log('角色列表', this.roleList)
    // this.roleList = res.permissions
  },
  methods: {
    // 封装获取指定员工的角色列表
    async  getRolecheckList(id) {
      const res = await employeeDatail(id)
      // console.log('指定员工的角色列表', res.roles)
      // this.RolecheckList = res.roles
      res.roles.forEach(item => {
        this.RolecheckList.push(item.id)
        // console.log('指定员工的角色列表', this.RolecheckList)
      })
    },

    // 关闭X和取消按钮
    closeFn() {
      this.$emit('update:dialogRole', false)
      // 清空内容
      this.RolecheckList = []
    },

    // 点击确定按钮
    async  comfirmFn(id) {
      await editEmployee({
        'id': id,
        'roles': this.RolecheckList
      })

      // 关闭弹窗
      this.closeFn()
    }

  }

}

</script>

<style>

</style>
